
package com.idealighter.game.games.happyfive.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.dictionary.dic.HappyfiveRoomDic;
import com.idealighter.game.dictionary.domain.HappyfiveRoomDomain;
import com.idealighter.game.games.happyfive.struct.HappyFiveRoom;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * . 欢乐五张数据管理 .
 * 
 * @date 2016年1月16日 下午8:23:41
 */
@Singleton
public class HappyFiveDataMgr {

  private static final Logger LOG = LoggerFactory.getLogger(HappyFiveDataMgr.class);

  private HappyfiveRoomDic roomDic;

  // 欢乐五张游戏房间
  private final Map<Integer, HappyFiveRoom> rooms;
  // 玩家座位
  private final Map<Long, HappyFiveRoom.Table.Seat> playerSeats = new HashMap<>();

  // 进入欢乐五张的玩家
  // private final Set<Long> players = new HashSet<>();

  @Inject
  public HappyFiveDataMgr(HappyfiveRoomDic roomDic) {
    this.roomDic = roomDic;
    this.rooms = createRooms();
  }

  /**
   * 创建房间 .
   * 
   * @return
   */
  private Map<Integer, HappyFiveRoom> createRooms() {
    Map<Integer, HappyFiveRoom> rooms = new HashMap<>();
    for (HappyfiveRoomDomain roomDom : roomDic.list()) {
      rooms.put(roomDom.getId(),
          new HappyFiveRoom(roomDom.getId(), roomDom.getName(), roomDom.getTable()));
      LOG.info("[欢乐五张]创建房间[{}][{}]", roomDom.getId(), roomDom.getName());
    }

    return rooms;
  }

  /**
   * 获取房间 .
   * 
   * @return
   */
  public Map<Integer, HappyFiveRoom> getRooms() {

    return rooms;
  }

  /**
   * 获取房间 .
   * 
   * @param roomId .
   * @return
   */
  public HappyFiveRoom getRoom(int roomId) {

    return rooms.get(roomId);
  }

  /**
   * 获取玩家的座位 .
   * 
   * @return
   */
  public HappyFiveRoom.Table.Seat getPlayerSeat(long playerId) {

    return playerSeats.get(playerId);
  }

  /**
   * 获取房间id .
   * 
   * @param tableId .
   * @return
   */
  public int getRoomId(int tableId) {

    return tableId / HappyFiveRoom.Table.MAX_TABLE;
  }

  /**
   * 获取牌桌 .
   * 
   * @param tableId .
   * @return
   */
  public HappyFiveRoom.Table getTable(int tableId) {
    HappyFiveRoom room = getRoom(getRoomId(tableId));

    if (room == null) {
      return null;
    } else {
      return room.tables.get(tableId);
    }

  }

  /**
   * 更新玩家座位 .
   * 
   * @param playerId 玩家id.
   * @param seat 座位信息.
   */
  public void updatePlayerSeats(long playerId, HappyFiveRoom.Table.Seat seat) {
    playerSeats.put(playerId, seat);
  }

  /**
   * 移除玩家座位 .
   * 
   * @param playerId 玩家id.
   */
  public void removePlayerSeat(long playerId) {
    playerSeats.remove(playerId);
  }

  /**
   * 房间里面的玩家 .
   * 
   * @return
   */
  /*
   * public . Set<Long> players() { return players; }
   */
}
